package org.tong.account.jpa.daos;

import io.seata.rm.tcc.api.BusinessActionContext;
import io.seata.rm.tcc.api.BusinessActionContextParameter;
import org.springframework.stereotype.Component;
import org.tong.account.jpa.AccountTccDaoI;

/**
 * @author
 * @date 2022-12-21 17:25
 */
@Component
public class AccountTccDao implements AccountTccDaoI {


    @Override
    public boolean prepare(BusinessActionContext businessActionContext, @BusinessActionContextParameter(paramName = "id") Long id,
                           @BusinessActionContextParameter(paramName = "amount") Long amount) {
        String xid = businessActionContext.getXid();
        long branchId = businessActionContext.getBranchId();
        System.out.println(String.format("AccountTccDao.prepare, xid:%s, branchId:%d", xid, branchId));

        System.out.println("AccountTccDao.prepare, id:" + id + " amount:" + amount);
        return true;
    }

    @Override
    public boolean commitTcc(BusinessActionContext businessActionContext) {
        String xid = businessActionContext.getXid();
        long branchId = businessActionContext.getBranchId();
        System.out.println(String.format("AccountTccDao.commit, xid:%s, branchId:%d", xid, branchId));


        Long id = Long.valueOf(businessActionContext.getActionContext("id").toString());
        Long amount = Long.valueOf(businessActionContext.getActionContext("amount").toString());
        System.out.println("AccountTccDao.commit, id:" + id + " amount:" + amount);
        return true;
    }


    @Override
    public boolean cancelTcc(BusinessActionContext businessActionContext) {
        String xid = businessActionContext.getXid();
        long branchId = businessActionContext.getBranchId();
        System.out.println(String.format("AccountTccDao.cancel, xid:%s, branchId:%d", xid, branchId));


        Long id = Long.valueOf(businessActionContext.getActionContext("id").toString());
        Long amount = Long.valueOf(businessActionContext.getActionContext("amount").toString());
        System.out.println("AccountTccDao.cancel, id:" + id + " amount:" + amount);
        return true;
    }
}
